package com.lanou.moving.adapter;

import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.TextView;

import com.lanou.moving.R;
import com.lanou.moving.entity.MVDaylyRoot;

import java.util.List;

import it.sephiroth.android.library.picasso.Picasso;

/**
 * Created by 梁树珍 on 15/11/3.
 */
public class MVDaylyAdapter extends BaseAdapter {
    private Context context;
    public static List<MVDaylyRoot.DataEntity> entityList;

    public MVDaylyAdapter(Context context, List<MVDaylyRoot.DataEntity> entityList) {
        this.context = context;
        this.entityList = entityList;
    }

    public void addDatas(List<MVDaylyRoot.DataEntity> datas) {
        this.entityList.addAll(datas);
        notifyDataSetChanged();
    }

    public void refreshDatas(List<MVDaylyRoot.DataEntity> datas) {
        this.entityList = datas;
        notifyDataSetChanged();
    }

    @Override
    public int getCount() {
        return entityList == null ? 0 : entityList.size();
    }

    @Override
    public Object getItem(int position) {
        return entityList == null ? null : entityList.get(position);
    }

    @Override
    public long getItemId(int position) {
        return position;
    }

    @Override
    public View getView(int position, View convertView, ViewGroup parent) {
        ViewHolder holder = null;
        if (convertView == null) {
            convertView = LayoutInflater.from(context).inflate(R.layout.mv_dayly_item, null);
            holder = new ViewHolder();
            holder.mv_dayly_title = (TextView) convertView.findViewById(R.id.mv_dayly_title);
            holder.mv_dayly_desc = (TextView) convertView.findViewById(R.id.mv_dayly_desc);
            holder.mv_dayly_comment = (TextView) convertView.findViewById(R.id.mv_dayly_comment);
            holder.tag_first = (TextView) convertView.findViewWithTag(R.id.tag_first);
            holder.tag_second = (TextView) convertView.findViewById(R.id.tag_second);
            holder.tag_third = (TextView) convertView.findViewById(R.id.tag_third);
            holder.mv_dayly_img = (ImageView) convertView.findViewById(R.id.mv_dayly_img);
            convertView.setTag(holder);
        } else {
            holder = (ViewHolder) convertView.getTag();
        }
        holder.mv_dayly_title.setText(entityList.get(position).getTitle());
        holder.mv_dayly_desc.setText(entityList.get(position).getDesc());
        holder.mv_dayly_comment.setText(String.valueOf(entityList.get(position).getBulletCount()));
        if (entityList.get(position).getTag().size() == 1){
        holder.tag_second.setText(entityList.get(position).getTag().get(0).getTagName());
        }else if (entityList.get(position).getTag().size() == 2){
            holder.tag_second.setText(entityList.get(position).getTag().get(0).getTagName());
        holder.tag_third.setText(entityList.get(position).getTag().get(1).getTagName());
        }else if (entityList.get(position).getTag().size() == 3){
            holder.tag_second.setText(entityList.get(position).getTag().get(0).getTagName());
            holder.tag_third.setText(entityList.get(position).getTag().get(1).getTagName());
            holder.tag_second.setText(entityList.get(position).getTag().get(2).getTagName());
        }
        Picasso.with(context).load(entityList.get(position).getBigPicUrl()).into(holder.mv_dayly_img);
        return convertView;
    }

    class ViewHolder {
        private TextView mv_dayly_title, mv_dayly_desc, mv_dayly_comment;
        private ImageView mv_dayly_img;
        private TextView tag_first, tag_second, tag_third;
    }
}
